#pragma once

#include "InetAddr.hpp"
#include "Log.hpp"

using namespace LogArea;
class UserInterFace
{
public:
    virtual ~UserInterFace() = default;
    virtual void SendTo() = 0;
    virtual bool operator==(const InetAddr &peer) = 0;
};

class User : public UserInterFace
{
public:
    User(const InetAddr &peer) : _user_addr(peer)
    {
    }
    void SendTo() override
    {
    }

    bool operator==(const InetAddr &peer) override
    {
        return _user_addr == peer;
    }

    InetAddr &GetAddr()
    {
        return _user_addr;
    }
    ~User()
    {
    }

private:
    InetAddr _user_addr;
};

// 管理用户, 先描述, 再组织
class UserManager
{
public:
    // 添加用户
    void AddUser(InetAddr &user)
    {
        LockGuard lockguard(_mutex);
        for (auto &it : _user_list)
        {
            if (*it == user)
                return;
        }
        _user_list.push_back(std::make_shared<User>(user));
        PrintUser();
    }

    void DelUser(InetAddr &user)
    {
        auto pos = remove_if(_user_list.begin(), _user_list.end(), [&user](std::shared_ptr<UserInterFace> &_user)
                             { return *_user == user; });

        _user_list.erase(pos, _user_list.end());
        PrintUser();
    }

    void PrintUser()
    {
        for (auto &it : _user_list)
            LOG(LogLevel::INFO) << "在线用户" << it->GetAddr().GetIp();
    }

private:
    std::list<std::shared_ptr<User>> _user_list;

    MyMutex _mutex;
};
